﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Restaurant.DAO
{
    public class NhaHangDB
    {
        private SqlConnection conn = KetNoiDB.TaoKetNoi();
        public DataTable LoadNH()
        {
            conn.Open();
            string strCmd = "dbo.spLoadNH";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            conn.Close();
            return dbtab;
        }

        //============ Dirty Read (Load) ===========================//
        public DataTable LoadNHDrityreadError()
        {
            conn.Open();
            string strCmd = "dbo.spDirtyRead_TimNH_Error";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            conn.Close();
            return dbtab;
        }

        public DataTable LoadNHDrityread()
        {
            conn.Open();
            string strCmd = "dbo.spDirtyRead_TimNH_Fixed";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            conn.Close();
            return dbtab;
        }
        //===================================================//

        //================= Unrepeatable Read (Load)===============//
        public DataTable LoadNHUnrepeatError(ref int soluong)
        {
            conn.Open();
            string strCmd = "dbo.spUnrepeatable_TimNH_Error";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
            cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
            conn.Close();
            return dbtab;
        }

        public DataTable LoadNHUnrepeat(ref int soluong)
        {
            conn.Open();
            string strCmd = "dbo.spUnrepeatable_TimNH_Fixed";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
            cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
            conn.Close();
            return dbtab;
        }
        //===================================================//

        //================= Phantom (Load)===============//
        public DataTable LoadNHPhantomError(ref int soluong)
        {
            conn.Open();
            string strCmd = "dbo.spPhantom_TimNH_Error";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
            cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
            conn.Close();
            return dbtab;
        }

        public DataTable LoadNHPhantom(ref int soluong)
        {
            conn.Open();
            string strCmd = "dbo.spPhantom_TimNH_Fix";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
            cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
            conn.Close();
            return dbtab;
        }
        //===================================================//

        //============== Dirty Read (Cap Nhat)=========================//
        public bool CapNhatNHDirtyread(int ma, string ten, string diachi, string sodt)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spDirtyRead_SuaNH_Fixed";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@diachi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sodt", sodt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool CapNhatNHDirtyreadError(int ma, string ten, string diachi, string sodt)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spDirtyRead_SuaNH_Error";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@diachi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sodt", sodt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //=============================================================//

        //============== Unrepeat (Cap Nhat)=========================//
        public bool CapNhatNHUnrepeat(int ma, string ten, string diachi, string sodt)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spUnrepeatable_SuaNH_Fix";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@diachi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sodt", sodt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool CapNhatNHUnrepeatError(int ma, string ten, string diachi, string sodt)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spUnrepeatable_SuaNH_Error";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@diachi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sodt", sodt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //=============================================================//

        //============== Phantom (Cap Nhat)=========================//
        public bool CapNhatNHPhantom(int ma, string ten, string diachi, string sodt)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spPhantom_SuaNH_Fix";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@diachi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sodt", sodt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool CapNhatNHPhantomError(int ma, string ten, string diachi, string sodt)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spPhantom_SuaNH_Error";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@diachi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sodt", sodt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //=============================================================//

        public bool ThemNH(string ten, string diachi, string sodt)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spThemNH";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@diachi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sodt", sodt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public DataTable TimNH(int ma)
        {
            conn.Open();
            string strCmd = "dbo.spTimNH";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ma", ma));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            conn.Close();
            return dbtab;
        }

        public DataTable LayMaNHTheoMaKhu(int maKhu)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spLayMaNHTheoMaKhu";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKhu", maKhu));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool CapNhatNH(int ma, string ten, string diachi, string sodt)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spSuaNH";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@diachi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sodt", sodt));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaNH(int ma)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spXoaNH";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public DataTable LayTenNH(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayTenNH";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DataTable LayMaNH(string ten)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayMaNH";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
